<div class="clear"></div>
<center>
	<form id="signupForm" name="signupForm"  method="post" class="form-horizontal" role="form">
		<div class="form-group">
		<label class="control-label col-sm-3"><strong>User Name:</strong></label>
			<div class="col-sm-9">
			<input class="input-btn usr-name" type="text" id="uname" name="uname" class="form-control" placeholder="Full Name"/>&nbsp;&nbsp;&nbsp;<span style="color:red" id="erroruname"></span></div>
		</div>
		<div class="form-group">
		<label class="control-label col-sm-3"><strong>Company name:</strong></label>
		<div class="col-sm-9">
			<input class="input-btn usr-name" type="text" id="cname" name="cname" class="form-control" placeholder="Company Name" />&nbsp;&nbsp;&nbsp;<span style="color:red" id="errorcname"></span>
			</div>
		</div>div
		<div class="form-group">
		<label class="control-label col-sm-3"><strong>Company contact number:</strong></label>
			<div class="col-sm-9">
			<input class="input-btn usr-name" type="text" id="ccnumber" name="ccnumber" class="form-control" placeholder="Company Contact Number" onblur="usernameVal()"/>&nbsp;&nbsp;&nbsp;<span style="color:red" id="errorccnumber"></span>
			</div>
		</div>
		<div class="form-group">
		<label class="control-label col-sm-3"><strong>Email:</strong></label>
			<div class="col-sm-9">
			<input class="input-btn usr-name" type="text" id="email" name="email" class="form-control" placeholder="Email"/>
			&nbsp;&nbsp;&nbsp;<span style="color:red" id="erroremail1"></span><span style="color:green" id="erroremail2"></span><span style="color:red" id="erroremail"></span>
		</div>
		</div>
		<div class="form-group">
		<label class="control-label col-sm-3"><strong>Phone Number:</strong></label>
			<div class="col-sm-9">
			<input class="input-btn usr-name" type="text" id="pnumber" name="pnumber" class="form-control" placeholder="Phone Number"/>
			&nbsp;&nbsp;&nbsp;<span style="color:red" id="errorpnumber"></span>
		</div></div>
		<div class="form-group">
		<label class="control-label col-sm-3"><strong>Country Name:</strong></label>
			<div class="col-sm-9">
			<select class="grid_6 column omega form-control" name="country" id="country" style="">
				<option value="">-Select a country</option>	
				<?php if(isset($this->countries) && count($this->countries)>0){foreach($this->countries as $Countries){ ?>
				<option value="<?php echo $Countries->country_id?>"><?php echo $Countries->nicename?></option>
				<?}}?>
			</select>&nbsp;&nbsp;&nbsp;<span style="color:red" id="errorcountry"></span>
		</div></div>
		<div class="form-group">
		<label class="control-label col-sm-3"><strong>State Name:</strong></label>
			<div class="col-sm-9">
			<select class="grid_6 column omega form-control" name="state" id="state">
				<option value="">-Select a state</option>										
			</select>&nbsp;&nbsp;&nbsp;<span style="color:red" id="errorstate"></span>
		</div></div>
		<div class="form-group">
		<label class="control-label col-sm-3"><strong>City:</strong></label>
			<div class="col-sm-9">
			<input class="input-btn usr-name" type="text" id="city" name="city" class="form-control" placeholder="Please enter city"/>
			&nbsp;&nbsp;&nbsp;<span style="color:red" id="errorcity"></span>
		</div></div>
		<div class="form-group">
		<label class="control-label col-sm-3"><strong>Address:</strong></label>
			<div class="col-sm-9"><textarea class="input-btn usr-name form-control" id="address" name="address"></textarea>
			&nbsp;&nbsp;&nbsp;<span style="color:red" id="erroraddress"></span>
		</div></div>
		<div class="form-group">
		
		<label class="control-label col-sm-3"><strong>Pincode Number:</strong></label>
		<div class="col-sm-9">	<input class="input-btn usr-name" type="text" id="pcode" name="pcode" class="form-control" placeholder="Please enter pincode"/>
			&nbsp;&nbsp;&nbsp;<span style="color:red" id="errorpcode"></span>
		</div></div>
		<div class="form-group">
		<label class="control-label col-sm-3"><strong>Type of user:</strong></label>
			<div class="col-sm-9">
			<select class="grid_6 column omega form-control" name="userr_type" id="userr_type">
				<option value="">-Select a user type</option>										
				<option value="admin">Admin</option>										
				<option value="user">User/Company</option>										
			</select>&nbsp;&nbsp;&nbsp;<span style="color:red" id="errorusertype"></span>
		</div></div>
		
		<div class="registerBtnOver btn2Bg btn2b form-group">
		<a  id="reg_button" onclick="validate()" class="registerBtn btn btn-danger" href="javascript:void(0);" role="button">Register</a>
		<a class="close registerBtn btn btn-danger" id="cancel" href="javascript:void(0);" role="button">Back</a>
		<span style="display:none;" id="loadding"><img src="images/loader16.gif"></span></div>
		<span style="color:red" id="regAuthmail"></span>
		<input type="hidden" id="hidemailerror" name="hidemailerror" value="1"/>
	</form>
</center>
<script type="text/javascript">
$('#email').focusout(function(e){
	var emailcheck=$('#email').val();
	if(emailcheck!==''){
	if(checkEmail(emailcheck)==false)
	{
		$('#erroremail1').html('');
		$('#erroremail2').html('');
		$('#erroremail').html('*Email Format is not correct');		
	}
	else{	
	$.ajax({
		type:'POST',
		url:  '/users/checkregemail',
		data:{email:emailcheck,type:'register'},
		success: function(data){
			if(data.output=='success'){
				$('#hidemailerror').val('1');
				$('#erroremail2').html('');
				$('#erroremail').html('');
				$('#erroremail1').html('*Already Email Exits');
			}else{
				$('#hidemailerror').val('0');
				$('#erroremail1').html('');
				$('#erroremail').html('')
				$('#erroremail2').html('*valid Email');
			}
		}
	});
	}
  }
});
function validate(){
	var flag=true;	
	var emailcheck=$('#email').val();
	var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	var letters = /^[a-zA-Z ]*$/; 
	var numbers = /^[0-9]+$/;
	var username=$('#uname').val();
	var companyname=$('#cname').val();
	var companycontactnumber=$('#ccnumber').val();
	var pnumber=$('#pnumber').val();
	var country=$('#country').val();
	var state=$('#state').val();
	var city=$('#city').val();
	var address=$('#address').val();
	var pincode=$('#pcode').val();
	var userr_type=$('#userr_type').val();
	
	if(username==""){
		$('#erroruname').html('Required');	
		flag=false;
	}else if(!username.match(letters)){
		$('#erroruname').html('');
		$("#erroruname").html("Characters only");
		flag=false;
	}else{
		$('#erroruname').html('');
	}
	if(companycontactnumber!=""){
	 if(!companycontactnumber.match(numbers)){
	    $('#errorccnumber').html('numbers only');
		flag=false;
	}else if(companycontactnumber.length<10 || companycontactnumber.length>10 ){
		$('#errorccnumber').html('valid phonenumber');
		flag=false;
	}}else{
		$('#errorccnumber').html('');
	}
	if($('#email').val()==""){
		$('#erroremail1').html('');
		$('#erroremail').html('*Email Required');
		flag=false;
	}else if(checkEmail(emailcheck)==false){
		$('#erroremail1').html('');
		$('#erroremail2').html('');
		$('#erroremail').html('*Email Format is not correct');	
		flag=false;
	}else if($('#hidemailerror').val()=='1'){
		$('#erroremail2').html('');
		$('#erroremail1').html('*Already Email Exits');
		flag=false;
	}else{
		$('#erroremail').html('');
	}	
	if(pnumber==""){
		$('#errorpnumber').html('Required');
		flag=false;
	}else if(!pnumber.match(numbers)){
		$('#errorpnumber').html('numbers only');
		flag=false;
	}else if(pnumber.length<10 || pnumber.length>10 ){
		$('#errorpnumber').html('valid phonenumber');
		flag=false;
	}else{
		$('#errorpnumber').html('');
	}
	if(country==""){
		$('#errorcountry').html('Required');
		flag=false;
	}else{
		$('#errorcountry').html('');
	}
    if(state==""){
		$('#errorstate').html('Required');
		flag=false;
	}else{
		$('#errorstate').html('');
	}
	if(city==""){
		$('#errorcity').html('Required');
		flag=false;
    }else{
        $('#errorcity').html('');
	}
	if(address==""){
		$('#erroraddress').html('Required');
		flag=false;
	}else{
		 $('#erroraddress').html('');
	}
	if(pincode==""){
		$('#errorpcode').html('Required');	
		flag=false;
	}else if(!pincode.match(numbers)){
		$('#errorpcode').html('numbers only');	
		flag=false;
	}else{
		$('#errorpcode').html('');
	}
	if(userr_type==""){
		$('#errorusertype').html('Required');	
		flag=false;
	}else{
		$('#errorusertype').html('');	
	}
	if(flag==false){ 
		return false;
	}else{		
		$("#reg_button").removeAttr("onclick");
		document.getElementById('reg_button').style.cursor = 'none';
		$('#loadding').show();	
		$.ajax({
			type:	'POST',
			url:    '/users/register',
			data:	$( "#signupForm" ).serialize(),
			success: function(data){
				$('#loadding').hide();	
				if(data.output=='success'){
					if(data.userReview=='yes'){
						window.location=data.sessionUrl;
					}else{
						$('#regAuthmail').html('Email Verification Link Sent To Your Email!.');	
					}
				}else{
					$('#regAuthmail').html('Error In Sent Mail!');
				}
			}
		});
	}	
}
	
function checkEmail(emailStr) {
	if (emailStr.length == 0) {
		return true;
	}
	var emailPat=/^(.+)@(.+)$/;
	var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]";
	var validChars="\[^\\s" + specialChars + "\]";
	var quotedUser="(\"[^\"]*\")";
	var ipDomainPat=/^(\d{1,3})[.](\d{1,3})[.](\d{1,3})[.](\d{1,3})$/;
	var atom=validChars + "+";
	var word="(" + atom + "|" + quotedUser + ")";
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
	var domainPat=new RegExp("^" + atom + "(\\." + atom + ")*$");
	var matchArray=emailStr.match(emailPat);
	if (matchArray == null) {
		return false;
	}
	var user=matchArray[1];
	var domain=matchArray[2];
	if (user.match(userPat) == null) {
		return false;
	}
	var IPArray = domain.match(ipDomainPat);
	if (IPArray != null) {
		for (var i = 1; i <= 4; i++) {
			if (IPArray[i] > 255) {
				return false;
			}
		}
		return true;
	}
	var domainArray=domain.match(domainPat);
	if (domainArray == null) {
		return false;
	}
	var atomPat=new RegExp(atom,"g");
	var domArr=domain.match(atomPat);
	var len=domArr.length;
	if ((domArr[domArr.length-1].length < 2) ||(domArr[domArr.length-1].length > 3)) {
		return false;
	}
	if (len < 2) {
		return false;
	}
	return true;
}
$('#cancel').click(function(e) { 
			$.ajax({
				url: '/users/login',
				success: function(data){
				$("#sign_up").lightbox_me({centered: true, onLoad: function() {
					
				}});
				e.preventDefault();
				$("#sign_up").html(data);
				$("#sign_up").find("input:first").focus();
				}
			});
		
	});
</script>
<style type="text/css">
    .hidreg_a {        
        cursor-pointer:  none;
    }
</style>
<script>
	$("#country").change(function(){ 		
		var countryid=$("#country").val();	
		$.ajax({
			type:	'POST',
			url:   	'/users/get-states',
			data:	{countryid:countryid},
			success: function(data){
				if(data.output=='success'){ 				
					$("#state").html(data.statenames);
				}							
			}
		});
	});
</script>









